package executors.completable.future;

import java.io.IOException;
import java.util.concurrent.*;

public class ApplyToEitherTest001 {
  public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    
    CompletableFuture<Integer> taskC = CompletableFuture.supplyAsync(() -> {
      System.out.println("任务A");
      try {
        TimeUnit.SECONDS.sleep(1);
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      }
      return 78;
    }, executorService).applyToEither(CompletableFuture.supplyAsync(() -> {
      System.out.println("任务B");
      try {
        TimeUnit.SECONDS.sleep(2);
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      }
      return 66;
    }), resultFirst -> {
      System.out.println("任务C");
      return resultFirst;
    });
    
    
    System.out.println(taskC.get());
  }
}
